package ro.gateway.aida.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.AIDASector;
import ro.gateway.aida.obj.GenericActivityProperty;
import ro.gateway.aida.obj.db.AIDASectorPersMan;
import ro.gateway.aida.obj.db.OrganizationDB;
import ro.gateway.aida.srv.EditorServlet;
import ro.gateway.aida.utils.HttpUtils;
import ro.gateway.aida.utils.SortedList;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;



/**
 * <p>Title: Romanian Aida</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: eRomania Gateway</p>
 * @author Mihai Postelnicu
 * @version 0.1
 * @deprecated
 */
public class ActivitySelectorServlet extends EditorServlet
{
      public static final String PNAME_SECT = "sector selected";
      public static final String PNAME_SUBSECT = "subsector selected";
      public static final String PNAME_ACTSEL = "list of selected activities (request)";
      public static final String PNAME_ACTSELS = "lists of selected activities (session)";
      public static final String PV_SCR_READY = "redirect to origin of request";
      public static final String PNAME_ACTKEY = "project select key (request)";
      public static final String PNAME_BEANS = "SELECTING_PROJECTS";
      public static final String PNAME_ORGLIST = "Organizattion list (request)";
      public static final String JSP_PAGE = "activitysel.jsp";
      public static final String PNAME_ROOTSECTORLIST = "Root Sectors list (request)";
      public static final String PNAME_SECTORLIST = "Sectors list (request)";
      public static final String PNAME_ERRORS = "errors";
      public static final String PNAME_ACTLIST = "Activities list (request)";

      public ActivitySelectorServlet()
      {
      }

      protected void edit( HttpServletRequest request, HttpServletResponse response )
            throws javax.servlet.ServletException, java.io.IOException
      {
            PersistenceToken token = PersistenceManager.
                  tokenLookup( this.getServletContext() );
            generateCommon( request );

            Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
            String lang = HttpUtils.getValidTrimedString( request, "lang", "ro" );

      }

      protected void other_action_performed( HttpServletRequest request,
                                             HttpServletResponse response, String action )
            throws javax.servlet.ServletException, java.io.IOException
      {
            /**@todo Implement this ro.gateway.aida.srv.EditorServlet abstract method*/
      }

      protected void confirm( HttpServletRequest request,
                              HttpServletResponse response )
            throws javax.servlet.ServletException, java.io.IOException
      {
            Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );

            //Vector actsel = (Vector) bean.get(PNAME_ACTSEL);
            Vector actsel = new Vector();
            bean.put( PNAME_ACTSEL, actsel );
            reload( PV_SCR_EDIT, request, response );
      }

      public String getPNAME_BEANS()
      {
            return PNAME_BEANS;
      }

      protected void save( ServletContext application, HttpServletRequest request,
                           HttpServletResponse response )
            throws javax.servlet.ServletException, java.io.IOException
      {
            HttpSession session = request.getSession();
            Hashtable beans = ( Hashtable ) request.getSession().getAttribute( PNAME_BEANS );
            Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
            String actkey = HttpUtils.getValidTrimedString( request, "actkey",
                  "anonymous" );
            Hashtable actsels = ( Hashtable ) beans.get( PNAME_ACTSELS );
            Vector actsel = ( Vector ) bean.get( PNAME_ACTSEL );
            actsels.put( actkey, actsel );
            reload( PV_SCR_READY, request, response );
      }

      public String getJSP_PAGE()
      {
            return JSP_PAGE;
      }

      protected void generateCommon( HttpServletRequest request)
      {
            HttpSession session = request.getSession();
            ServletContext application = this.getServletContext();
            PersistenceToken token = PersistenceManager.
                  tokenLookup(application);
            Hashtable beans = ( Hashtable ) session.getAttribute( PNAME_BEANS );
            Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
            String action = ( String ) bean.get( PNAME_ACTION );
            String key = ( String ) request.getAttribute( PNAME_KEY );
            String actkey = HttpUtils.getValidTrimedString( request, "actkey",
                  "anonymous" );
            String sectors = HttpUtils.getValidTrimedString( request, "sectors", null );
            String orgids = HttpUtils.getValidTrimedString( request, "orgids", null );
            LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage( request,
                  application );
            String lang = lang_bean.language;
            int rootsectselect = HttpUtils.getInt( request, "rootsectselect", -1 );
            int sectselect = HttpUtils.getInt( request, "sectselect", -1 );

            bean.put( PNAME_SECT, Integer.toString( rootsectselect ) );
            bean.put( PNAME_SUBSECT, Integer.toString( sectselect ) );

            //generate orglist
            OrganizationDB orgdb = OrganizationDB.getManager(token);
            SortedList orgList = ( SortedList ) bean.get( PNAME_ORGLIST );
            Vector sectorList = ( Vector ) bean.get( PNAME_SECTORLIST );

            if ( sectorList == null )
                  sectorList = new Vector();

            Vector rootSectorList = ( Vector ) bean.get( PNAME_ROOTSECTORLIST );

            Hashtable actsels = ( Hashtable ) beans.get( PNAME_ACTSELS );

            if ( actsels == null )
            {
                  actsels = new Hashtable();
                  beans.put( PNAME_ACTSELS, actsels );
            }

            Vector actsel = ( Vector ) bean.get( PNAME_ACTSEL );

            if ( actsel == null )
            {
                  //try to revive activity selector from session:
                  actsel = ( Vector ) actsels.get( actkey );

                  if ( actsel == null )
                        actsel = new Vector();

                  bean.put( PNAME_ACTSEL, actsel );
            }

            //lista cu activity titles care vor fi afisate dupa cautare
            Vector actlist = ( Vector ) bean.get( PNAME_ACTLIST );

            if ( actlist == null )
            {
                  actlist = new Vector();
                  bean.put( PNAME_ACTLIST, actlist );
            }

            if ( orgList == null )
            {
            	try {
                 orgList=orgdb.getList();
            	} catch(SQLException e) {
            		e.printStackTrace();
            	}
            }

            if ( rootsectselect != -1 )
            {
                  //subsector choose
                  try
                  {
                        sectorList = getSectorListAsVector( AIDASectorPersMan.getManager(token).
                              getAllByLangForParent(lang, rootsectselect ) );
                  }
                  catch ( SQLException ex2 )
                  {
                        ex2.printStackTrace();
                  }
            }

            if ( ( sectors != null ) && ( sectorList == null ) )
                  sectorList = AIDASectorPersMan.getManager(token).
                        getSectorsByID( sectors, lang);

            if ( rootSectorList == null )
            {
                  rootSectorList = new Vector();

                  AIDASector[] s = null;

                  try
                  {
                        s = AIDASectorPersMan.getManager(token).
                              getAllRootsByLang( lang );
                  }
                  catch ( SQLException ex1 )
                  {
                        ex1.printStackTrace();
                  }

                  rootSectorList = getSectorListAsVector( s );
            }

            bean.put( PNAME_ROOTSECTORLIST, rootSectorList );
            bean.put( PNAME_SECTORLIST, sectorList );
            bean.put( PNAME_ORGLIST, orgList );
            bean.put( PNAME_ACTKEY, actkey );
      }

      protected Vector getSectorListAsVector( AIDASector[] s )
      {
            Vector ret = new Vector();

            if ( s == null )
                  return ret;

            for ( int x = 0; x < s.length; x++ )
                  ret.add( s[ x ] );

            return ret;
      }

      protected void start_editing( ServletContext application,
                                    HttpServletRequest request, HttpServletResponse response )
            throws IOException, ServletException
      {
            super.start_editing( application, request, response );

            //generates common objects
            generateCommon( request);
      }
}
